var PreOrderModel = function (order, user) {
  BaseModel.call(this);

  var self = this;
  var dateFields = ["laterPhaseOne", "laterPhaseTwo", "laterPhaseThr", "laterPhaseFour"];
  var fn = {
    initData: function (order, user) {
      var fields = ['purchasePeople', 'currency', 'price', 'tax', 'materialName', 'materialType', 'color', 'colorDescription',
        'model', 'unit', 'defaultWarehouse', 'procurementCycles', 'orderId', 'companyOrderId', 'purchaseCount', 'createUser',
        'status', 'approvalUser', 'approvalTime', 'createTime', 'providerName', 'isApprovalUser', 'articleNo', 'factoryNo',
        'material'
      ];
      fields.pushArray(dateFields);
      self.setDataFromObj(order, fields, '');

      self.id = order.mainId;
      self.curstomer = order.customerBean;

      var isApprovalUser = self.isApprovalUser;
      var createUserId = order.createUserId;
      var isCanUpdate = isApprovalUser === 1 || createUserId === user.userId;
      self.isCanUpdate = isCanUpdate;

      var timeFields = ["createTime", "approvalTime"];
      timeFields.pushArray(dateFields);
      this.formatTimeData(timeFields);

      var laterPhaseDateList = self.getLaterPhaseDateList();
      laterPhaseDateList = _.map(laterPhaseDateList, 'date');
      self.maxLaterPhase = _.maxBy(laterPhaseDateList, function (date) {
        if (date === '') {
          return null;
        }
        return date;
      });

      self.totalMoney = this.getTotalMoney();
    },
    formatTimeData(fields) {
      for (var i = 0; i < fields.length; i++) {
        var field = fields[i];
        var time = self[field];
        if (time === null || time === '') {
          time = '';
        } else {
          time = moment(time).valueOf();
        }
        self[field] = time;
      }
    },
    getTotalMoney: function () {
      var purchaseCount = self.purchaseCount;
      var price = self.price;
      return _.round(purchaseCount * price, globalConfig.pricePrecision);
    }
  };

  this.getLaterPhaseDateList = function () {
    var laterPhaseDateList = [];
    for (var i = 0; i < dateFields.length; i++) {
      var field = dateFields[i];
      var date = self[field];
      if (i !== 0 && date === '') {
        break;
      }

      var obj = {
        date: date,
        readonly: true,
        canDel: false
      };

      if (i === 0 && date === '') {
        obj.readonly = false;
      }

      laterPhaseDateList.push(obj);
    }
    return laterPhaseDateList;
  };

  this.setLaterPhaseDateList = function(laterPhaseDateList){
    for (var i = 0; i < laterPhaseDateList.length; i++) {
      var laterPhaseDate = laterPhaseDateList[i].date;
      var field = dateFields[i];
      this[field] = laterPhaseDate;
    }
  };

  this.getUpdateObject = function (laterPhaseDateList) {
    var postData = {
      mainId: self.id
    };

    for (var i = 0; i < laterPhaseDateList.length; i++) {
      var laterPhaseDate = laterPhaseDateList[i].date;
      var field = dateFields[i];
      if (laterPhaseDate === '') {
        return field;
      }
      laterPhaseDate = moment(laterPhaseDate).format('YYYY-MM-DD HH:mm:ss');
      postData[field] = laterPhaseDate;
    }
    return postData;
  };

  this.getSearchObject = function () {
    var postData = {};
    var changeFields = ['materialName', 'model', 'color', 'providerName'];
    this.setPostData(postData, changeFields, true, true);
    if (postData.color === '') {
      delete postData.color;
    }
    return postData;
  };

  this.getObject = function () {
    var postData = {
      mainId: this.id
    };
    var fields = ['materialName', 'model', 'providerName', 'color', 'colorDescription'];
    this.setPostData(postData, fields, true, true);
    return postData;
  };

  this.filterData = function (search) {
    var curstomer = this.curstomer;
    if (curstomer === null) {
      curstomerId = '';
    } else {
      curstomerId = curstomer.id;
    }

    var materialName = search.materialName;
    if (materialName === '') {
      return curstomerId === search.curstomerId;
    }
    return this.materialName === materialName && curstomerId === search.curstomerId;
  };

  fn.initData(order, user);
};